package com.webkitlib.base;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.view.Window;
import android.widget.Toast;

import com.webkitlib.util.KAtyManager;
import com.webkitlib.util.KLoger;
import com.webkitlib.util.KToast;

/**
 * 基类
 *
 * @author 韩伟光
 * @version 1.0
 * @since 2016-1-18
 */
public class BaseActivity extends FragmentActivity {
    private Fragment currentKJFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        KAtyManager.create().addActivity(this);// 添加进管理器
        KLoger.debug("BaseActivity", this.getClass().getName() + "--> " + "onCreate");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        KLoger.debug("BaseActivity", this.getClass().getName() + "--> " + "onDestroy");
    }

    @Override
    protected void onPause() {
        super.onPause();
        KLoger.debug("BaseActivity", this.getClass().getName() + "--> " + "onPause");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        KLoger.debug("BaseActivity", this.getClass().getName() + "--> " + "onRestart");
    }

    @Override
    protected void onResume() {
        super.onResume();
        KLoger.debug("BaseActivity", this.getClass().getName() + "--> " + "onResume");
    }

    @Override
    protected void onStart() {
        super.onStart();
        KLoger.debug("BaseActivity", this.getClass().getName() + "--> " + "onStart");
    }

    @Override
    protected void onStop() {
        super.onStop();
        KLoger.debug("BaseActivity", this.getClass().getName() + "--> " + "onStop");
    }


    /**
     * 提示
     *
     * @param message
     */
    protected void ktoast(String message, int textColor, int backColor) {
        KToast.showToast(this, message, Toast.LENGTH_SHORT, textColor, backColor);
    }

    public void changeFragment(int resView, Fragment targetFragment) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        if (!targetFragment.isAdded()) {
            KLoger.d("==添加fragment==", "tag的名字为: " + targetFragment.getClass().getSimpleName());
            transaction.add(resView, targetFragment, targetFragment.getClass().getSimpleName());
        }
        if (targetFragment.isHidden()) {
            transaction.show(targetFragment);
        }
        if (currentKJFragment != null && currentKJFragment.isVisible()) {
            transaction.hide(currentKJFragment);
        }
        currentKJFragment = targetFragment;
        transaction.commit();
    }

}
